﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.franchise.display {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import org.aswing.*;
    import Classes.*;
    import Classes.util.*;
    import flash.filters.*;
    import org.aswing.geom.*;
    import Display.*;
    import flash.text.*;
    import Modules.franchise.data.*;
    import Display.aswingui.*;
    import L10n.*;
    import Modules.franchise.transactions.*;

    public class FranchiseCard extends DataItemCell {

        private static const ROW_RED:Number = 0.9;
        private static const ROW_GREEN:Number = 0.94;
        private static const ROW_BLUE:Number = 0.98;
        private static const BUTTON_HEIGHT:int = 35;
        private static const BUTTON_WIDTH:int = 165;

        protected var m_content:DisplayObject;
        private var m_cardPanel:JPanel;
        private var m_headerPanel:JPanel;
        private var m_bodyPanel:JPanel;
        private var m_footerPanel:JPanel;
        private var m_itemIconHolder:JPanel;
        private var m_itemIconPane:JPanel;
        private var m_color:int;
        private var m_position:int;
        private var m_status:String;
        private var m_collectAmount:Number;
        private var m_collectAmountBeforeStars:Number;
        private var m_witherAmount:Number;
        private var m_franchiseExpansion:FranchiseExpansionData;

        public function FranchiseCard(_arg1:int=0, _arg2:int=0, _arg3:LayoutManager=null){
            this.m_color = _arg1;
            this.m_position = _arg2;
            super(new SoftBoxLayout(SoftBoxLayout.X_AXIS, 0, SoftBoxLayout.LEFT));
        }
        override public function setCellValue(_arg1):void{
            var _local2:FranchiseExpansionData = (_arg1 as FranchiseExpansionData);
            this.setExpansionCellValue(_local2);
        }
        private function setExpansionCellValue(_arg1:FranchiseExpansionData):void{
            var _local2:Player;
            var _local3:String;
            this.m_franchiseExpansion = _arg1;
            switch (_arg1.starRating){
                case StarRatingComponent.NONE:
                    this.m_status = FranchiseMenu.STATUS_BLANK;
                    break;
                case StarRatingComponent.DISABLED:
                    this.m_status = FranchiseMenu.STATUS_ADD_NEIGHBOR;
                    break;
                default:
                    if (((!(this.m_franchiseExpansion.isOpen())) || ((this.m_franchiseExpansion.timeLastSupplied > this.m_franchiseExpansion.timeLastOperated)))){
                        this.m_status = FranchiseMenu.STATUS_NEED_REMIND;
                    } else {
                        if ((GlobalEngine.getTimer() / 1000) < (this.m_franchiseExpansion.timeLastSupplied + FranchiseMenu.dailyCycleDelta)){
                            this.m_status = FranchiseMenu.STATUS_COLLECT_WAITING;
                        } else {
                            this.m_status = FranchiseMenu.STATUS_NEED_REFILL;
                        };
                    };
                    _local2 = Global.player.findFriendById(_arg1.locationUid);
                    if (_local2){
                        _local3 = _local2.snUser.picture;
                        m_loader = LoadingManager.load(_local3, onIconLoad, LoadingManager.PRIORITY_HIGH);
                    };
            };
            this.buildCell();
        }
        protected function buildCell():void{
            var _local5:Array;
            var _local6:Array;
            this.m_cardPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.TOP);
            this.m_headerPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.TOP);
            this.m_bodyPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            this.m_footerPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local1:DisplayObject = (new (FranchiseMenuUI.m_assetDict["fr_row_top"])() as DisplayObject);
            var _local2:DisplayObject = (new (FranchiseMenuUI.m_assetDict["fr_row_mid"])() as DisplayObject);
            var _local3:DisplayObject = (new (FranchiseMenuUI.m_assetDict["fr_row_bot"])() as DisplayObject);
            if ((this.m_position % 4) == 0){
                this.m_content = _local3;
            } else {
                this.m_content = _local2;
            };
            if ((this.m_color % 2)){
                _local5 = this.m_content.filters;
                _local6 = new Array();
                _local6 = _local6.concat([ROW_RED, 0, 0, 0, 0]);
                _local6 = _local6.concat([0, ROW_GREEN, 0, 0, 0]);
                _local6 = _local6.concat([0, 0, ROW_BLUE, 0, 0]);
                _local6 = _local6.concat([0, 0, 0, 1, 0]);
                _local5.push(new ColorMatrixFilter(_local6));
                this.m_content.filters = _local5;
            };
            var _local4:MarginBackground = new MarginBackground(this.m_content, new Insets(0, 0, 0, 0));
            this.m_cardPanel.setBackgroundDecorator(_local4);
            this.m_cardPanel.setPreferredSize(new IntDimension(this.m_content.width, (this.m_content.height - 8)));
            this.m_cardPanel.setMinimumSize(new IntDimension(this.m_content.width, (this.m_content.height - 8)));
            this.m_cardPanel.setMaximumSize(new IntDimension(this.m_content.width, (this.m_content.height - 8)));
            ASwingHelper.prepare(this.m_cardPanel);
            this.makeHeaderPanel();
            this.makeBodyPanel();
            this.makeFooterPanel();
            this.append(this.m_cardPanel);
            this.setPreferredWidth((this.m_content.width - 10));
            this.setMinimumWidth((this.m_content.width - 10));
            this.setMaximumWidth((this.m_content.width - 10));
            ASwingHelper.prepare(this);
        }
        private function makeHeaderPanel():void{
            var _local4:JLabel;
            var _local8:Player;
            var _local9:String;
            var _local1:DisplayObject = (new (FranchiseMenuUI.m_assetDict["fr_rule"])() as DisplayObject);
            var _local2:AssetPane = new AssetPane(_local1);
            this.m_itemIconHolder = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            this.m_itemIconPane = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            this.m_itemIconHolder.append(this.m_itemIconPane);
            this.m_itemIconHolder.setMinimumSize(new IntDimension(50, 50));
            this.m_itemIconHolder.setMaximumSize(new IntDimension(50, 50));
            this.m_itemIconHolder.setPreferredSize(new IntDimension(50, 50));
            var _local3:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.LEFT);
            var _local5:Number = TextFieldUtil.getLocaleFontSize(16, 12, null);
            switch (this.m_status){
                case FranchiseMenu.STATUS_ADD_NEIGHBOR:
                    _local4 = new JLabel(ZLoc.t("Dialogs", "NextLocation"));
                    _local4.setFont(ASwingHelper.getBoldFont(_local5));
                    _local4.setForeground(new ASColor(FranchiseMenu.COLOR_MENU_GRAY));
                    break;
                case FranchiseMenu.STATUS_BLANK:
                    _local4 = new JLabel("");
                    break;
                default:
                    _local8 = Global.player.findFriendById(this.m_franchiseExpansion.locationUid);
                    _local9 = Global.franchiseManager.getMapOwnerFirstName(this.m_franchiseExpansion.locationUid);
                    _local4 = new JLabel(ZLoc.t("Main", "FriendNameSuffix", {name:ZLoc.tn(_local9)}));
                    _local4.setFont(ASwingHelper.getBoldFont(_local5));
                    LocaleHelper.localSetting.JLabel_setFont(_local4, EmbeddedArt.SYSTEM_FONT, _local5);
                    _local4.setForeground(new ASColor(FranchiseMenu.COLOR_MENU_STANDARD));
            };
            var _local6:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            _local6.append(ASwingHelper.verticalStrut(6));
            _local6.append(_local4);
            ASwingHelper.prepare(_local6);
            var _local7:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            _local7.appendAll(ASwingHelper.horizontalStrut(3), new StarRatingComponent(this.m_franchiseExpansion.starRating, true));
            _local3.append(_local6);
            _local3.append(_local7);
            this.m_headerPanel.append(ASwingHelper.horizontalStrut(5));
            this.m_headerPanel.append(this.m_itemIconHolder);
            this.m_headerPanel.append(_local3);
            this.m_headerPanel.setPreferredWidth(180);
            this.m_headerPanel.setMinimumWidth(180);
            this.m_headerPanel.setMaximumWidth(180);
            ASwingHelper.prepare(this.m_headerPanel);
            this.m_cardPanel.append(this.m_headerPanel);
            this.m_cardPanel.appendAll(_local2);
        }
        private function makeBodyPanel():void{
            var _local5:JTextField;
            var _local7:String;
            var _local12:String;
            this.m_bodyPanel.removeAll();
            var _local1:DisplayObject = (new (FranchiseMenuUI.m_assetDict["fr_rule"])() as DisplayObject);
            var _local2:AssetPane = new AssetPane(_local1);
            var _local3:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local4:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            var _local6:TextFormat = new TextFormat();
            _local6.font = EmbeddedArt.DEFAULT_FONT_NAME_BOLD;
            _local6.color = FranchiseMenu.COLOR_MENU_STANDARD;
            _local6.align = TextFormatAlign.CENTER;
            _local6.size = 14;
            var _local8:Player = Global.player.findFriendById(this.m_franchiseExpansion.locationUid);
            var _local9:String = Global.franchiseManager.getMapOwnerFirstName(this.m_franchiseExpansion.locationUid);
            switch (this.m_status){
                case FranchiseMenu.STATUS_NEED_REFILL:
                    _local12 = FranchiseMenu.selectedFranchise.saleCost.toString();
                    _local7 = ((ZLoc.t("Dialogs", "Status_NeedRefill1") + "\n") + ZLoc.t("Dialogs", "Status_NeedRefill2", {amount:ASwingHelper.colorize(_local12, 0xFF6A00)}));
                    break;
                case FranchiseMenu.STATUS_NEED_REMIND:
                    _local7 = ((ZLoc.t("Dialogs", "Status_NeedRemind1", {neighbor:_local9}) + "\n") + ZLoc.t("Dialogs", "Status_NeedRemind2"));
                    break;
                case FranchiseMenu.STATUS_DONE_REMIND:
                    _local7 = ((ZLoc.t("Dialogs", "Status_NeedRemind1", {neighbor:_local9}) + "\n") + ASwingHelper.colorize(ZLoc.t("Dialogs", "Status_NeedRemind2"), FranchiseMenu.COLOR_MENU_GRAY));
                    break;
                case FranchiseMenu.STATUS_ADD_NEIGHBOR:
                    _local7 = ZLoc.t("Dialogs", "Status_AddNeighbor");
                    break;
                case FranchiseMenu.STATUS_COLLECT_NOWITHER:
                    _local7 = ((ZLoc.t("Dialogs", "Status_CollectNoWither1", {
                        coin:ASwingHelper.colorize(this.m_collectAmountBeforeStars.toString(), 0xAA00),
                        star:ASwingHelper.colorize(this.m_franchiseExpansion.starRating.toString(), 0xAA00)
                    }) + "\n") + ZLoc.t("Dialogs", "Status_CollectNoWither2", {total:ASwingHelper.colorize(this.m_collectAmount.toString(), 0xAA00)}));
                    break;
                case FranchiseMenu.STATUS_COLLECT_WITHER:
                    _local7 = ((ZLoc.t("Dialogs", "Status_CollectWither1", {
                        coin:ASwingHelper.colorize(this.m_collectAmountBeforeStars.toString(), 0xAA00),
                        star:ASwingHelper.colorize(this.m_franchiseExpansion.starRating.toString(), 0xAA00),
                        total:ASwingHelper.colorize(this.m_collectAmount.toString(), 0xAA00)
                    }) + "\n") + ZLoc.t("Dialogs", "Status_CollectWither2", {coin:ASwingHelper.colorize(this.m_witherAmount.toString(), 0xAA0000)}));
                    break;
                case FranchiseMenu.STATUS_COLLECT_WAITING:
                    _local7 = ZLoc.t("Dialogs", "Status_CollectWaiting");
                    break;
                case FranchiseMenu.STATUS_BLANK:
                    _local7 = "";
                    break;
                default:
                    _local7 = "";
            };
            _local5 = ASwingHelper.makeTextField("", EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 15, FranchiseMenu.COLOR_MENU_STANDARD);
            _local5.setWordWrap(true);
            _local5.setTextFormat(_local6);
            LocaleHelper.localSetting.TextField_adjust(_local5.getTextField(), {
                embedFonts:false,
                font:EmbeddedArt.SYSTEM_FONT
            });
            _local5.setHtmlText(_local7);
            var _local10 = 240;
            var _local11 = 260;
            if ((((((this.m_status == FranchiseMenu.STATUS_COLLECT_NOWITHER)) || ((this.m_status == FranchiseMenu.STATUS_COLLECT_WITHER)))) || ((this.m_status == FranchiseMenu.STATUS_COLLECT_WAITING)))){
                _local10 = 390;
                _local11 = 410;
            };
            _local5.setMinimumWidth(_local10);
            _local5.setMaximumWidth(_local10);
            _local5.setPreferredWidth(_local10);
            _local3.setMaximumHeight((this.m_content.height - 10));
            _local3.setMinimumHeight((this.m_content.height - 10));
            _local3.setPreferredHeight((this.m_content.height - 10));
            _local3.append(_local5);
            _local4.append(_local3);
            ASwingHelper.prepare(_local4);
            if ((((((this.m_status == FranchiseMenu.STATUS_COLLECT_NOWITHER)) || ((this.m_status == FranchiseMenu.STATUS_COLLECT_WITHER)))) || ((this.m_status == FranchiseMenu.STATUS_COLLECT_WAITING)))){
                this.m_bodyPanel.appendAll(_local4);
            } else {
                this.m_bodyPanel.appendAll(_local4, _local2, ASwingHelper.horizontalStrut(5));
            };
            _local4.setPreferredWidth(_local11);
            _local4.setMinimumWidth(_local11);
            _local4.setMaximumWidth(_local11);
            ASwingHelper.prepare(this.m_bodyPanel);
            this.m_cardPanel.append(this.m_bodyPanel);
        }
        private function makeFooterPanel():void{
            var _local3:CustomButton;
            this.m_footerPanel.removeAll();
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local4:DisplayObject = (new (FranchiseMenuUI.m_assetDict["fr_rule"])() as DisplayObject);
            var _local5:AssetPane = new AssetPane(_local4);
            var _local6:Number = TextFieldUtil.getLocaleFontSize(18, 14, null);
            switch (this.m_status){
                case FranchiseMenu.STATUS_NEED_REFILL:
                    _local3 = new CustomButton(ZLoc.t("Dialogs", "SupplyRun").toUpperCase(), null, "GreenButtonUI");
                    break;
                case FranchiseMenu.STATUS_NEED_REMIND:
                    _local3 = new CustomButton(ZLoc.t("Dialogs", "Remind").toUpperCase(), null, "OrangeButtonUI");
                    break;
                case FranchiseMenu.STATUS_DONE_REMIND:
                    _local3 = new CustomButton(ZLoc.t("Dialogs", "Remind").toUpperCase(), null, "GreyButtonUI");
                    break;
                case FranchiseMenu.STATUS_ADD_NEIGHBOR:
                    _local3 = new CustomButton(ZLoc.t("Dialogs", "AddNeighbor").toUpperCase(), null, "GreenButtonUI");
                    break;
                case FranchiseMenu.STATUS_COLLECT_NOWITHER:
                case FranchiseMenu.STATUS_COLLECT_WITHER:
                case FranchiseMenu.STATUS_COLLECT_WAITING:
                case FranchiseMenu.STATUS_BLANK:
            };
            if (_local3){
                _local3.setFont(ASwingHelper.makeFont(EmbeddedArt.TITLE_FONT, _local6));
                _local3.setPreferredSize(new IntDimension((BUTTON_WIDTH - 4), BUTTON_HEIGHT));
                _local3.setMinimumSize(new IntDimension((BUTTON_WIDTH - 4), BUTTON_HEIGHT));
                _local3.setMaximumSize(new IntDimension((BUTTON_WIDTH - 4), BUTTON_HEIGHT));
                _local2.appendAll(ASwingHelper.horizontalStrut(3), _local3, ASwingHelper.horizontalStrut(3));
                _local1.appendAll(_local2);
                _local3.addEventListener(MouseEvent.CLICK, this.onButtonClick, false, 0, true);
            };
            this.m_footerPanel.append(_local1);
            if (_local3){
                this.m_footerPanel.appendAll(ASwingHelper.horizontalStrut(6), _local5);
            } else {
                if ((((this.m_status == FranchiseMenu.STATUS_COLLECT_NOWITHER)) || ((this.m_status == FranchiseMenu.STATUS_COLLECT_WITHER)))){
                    this.m_footerPanel.appendAll(ASwingHelper.horizontalStrut(30), _local5);
                } else {
                    this.m_footerPanel.appendAll(ASwingHelper.horizontalStrut(173), _local5);
                };
            };
            ASwingHelper.prepare(this.m_footerPanel);
            this.m_cardPanel.append(this.m_footerPanel);
            ASwingHelper.prepare(this.m_cardPanel);
        }
        public function refreshCard():void{
            this.makeBodyPanel();
            this.makeFooterPanel();
        }
        public function setCollectInfo(_arg1:String, _arg2:Number):void{
            this.m_status = _arg1;
            this.m_collectAmount = _arg2;
        }
        private function onButtonClick(_arg1:MouseEvent):void{
            var _local2:Number;
            var _local3:Number;
            var _local4:Number;
            var _local5:FranchiseExpansionData;
            var _local6:int;
            switch (this.m_status){
                case FranchiseMenu.STATUS_NEED_REFILL:
                    if (Global.player.commodities.getCount(FranchiseMenu.selectedFranchise.commType) >= FranchiseMenu.selectedFranchise.saleCost){
                        Sounds.play("Collect");
                        _local2 = this.m_franchiseExpansion.starRating;
                        _local3 = this.m_franchiseExpansion.moneyCollected;
                        _local4 = 100;
                        if (this.m_franchiseExpansion.timeLastCollected){
                            _local6 = int(((GlobalEngine.getTimer() / 1000) - this.m_franchiseExpansion.timeLastCollected));
                            _local4 = Global.franchiseManager.mapElapsedToCompleteness((_local6 / 3600));
                        };
                        this.m_collectAmount = Math.floor(((_local3 * _local4) / 100));
                        this.m_collectAmountBeforeStars = Math.round((this.m_collectAmount / _local2));
                        this.m_witherAmount = Math.floor((_local3 - this.m_collectAmount));
                        this.setCollectInfo(FranchiseMenu.STATUS_COLLECT_NOWITHER, this.m_collectAmount);
                        Global.player.gold = (Global.player.gold + this.m_collectAmount);
                        Global.player.commodities.remove(FranchiseMenu.selectedFranchise.commType, FranchiseMenu.selectedFranchise.saleCost);
                        FranchiseMenu.menuUI.doSendSaleFlyout(this.m_collectAmount, _local2, _arg1.stageX, _arg1.stageY);
                        FranchiseMenu.menuUI.doSendSaleDoobers(this.m_collectAmount, _arg1.stageX, _arg1.stageY, false);
                        if (_local4 == 100){
                            this.m_status = FranchiseMenu.STATUS_COLLECT_NOWITHER;
                        } else {
                            this.m_status = FranchiseMenu.STATUS_COLLECT_WITHER;
                        };
                        GameTransactionManager.addTransaction(new TFranchiseSupply(FranchiseMenu.selectedFranchise.franchiseType, this.m_franchiseExpansion.locationUid, _local2));
                        GameTransactionManager.addTransaction(new TFranchiseCollect(FranchiseMenu.selectedFranchise.franchiseType, this.m_franchiseExpansion.locationUid, this.m_franchiseExpansion.moneyCollected));
                        this.refreshCard();
                        _local5 = Global.franchiseManager.model.getFranchise(FranchiseMenu.selectedFranchise.franchiseType, this.m_franchiseExpansion.locationUid);
                        Global.franchiseManager.collect(_local5.franchiseType, _local5.locationUid);
                    } else {
                        this.doTextFlyout(ZLoc.t("Dialogs", ("NotEnough" + FranchiseMenu.selectedFranchise.commType), {amount:FranchiseMenu.selectedFranchise.saleCost}), _arg1.stageX, _arg1.stageY);
                    };
                    break;
                case FranchiseMenu.STATUS_ADD_NEIGHBOR:
                    Sounds.play("Remind");
                    FrameManager.showTray("invite.php?ref=franchise_menu");
                    break;
                case FranchiseMenu.STATUS_NEED_REMIND:
                    FranchiseMenuUI.addFriendReminder(FranchiseMenu.selectedFranchise.franchiseType, this.m_franchiseExpansion.locationUid);
                    Sounds.play("Remind");
                    this.m_status = FranchiseMenu.STATUS_DONE_REMIND;
                    this.refreshCard();
                    break;
                case FranchiseMenu.STATUS_DONE_REMIND:
                    break;
                default:
                    this.doTextFlyout("OH NOES", _arg1.stageX, _arg1.stageY);
            };
        }
        override protected function initializeCell():void{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            var _local2:AssetPane = new AssetPane(m_itemIcon);
            _local1.append(_local2);
            this.m_itemIconPane.append(_local1);
        }
        private function doTextFlyout(_arg1:String, _arg2:int, _arg3:int):void{
            UI.displayStatus(_arg1, _arg2, _arg3);
        }

    }
}//package Modules.franchise.display 
